package top.easyblog.core.atomic;

import com.google.common.collect.Iterables;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import top.easyblog.common.request.QueryCompressCodesRequest;
import top.easyblog.core.dao.auto.mapper.CompressionCodeMapper;
import top.easyblog.core.dao.auto.model.CompressionCode;
import top.easyblog.core.dao.auto.model.CompressionCodeExample;
import top.easyblog.core.dao.custom.mapper.MyCompressionCodeMapper;
import top.easyblog.core.util.JsonUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author: frank.huang
 * @date: 2023-12-18 22:54
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AtomicCompressCodeService {

    private final CompressionCodeMapper mapper;

    public final MyCompressionCodeMapper myCompressionCodeMapper;


    public List<CompressionCode> queryListByRequest(QueryCompressCodesRequest request) {
        CompressionCodeExample example = generateExamples(request);
        if (Objects.nonNull(request.getLimit())) {
            example.setLimit(request.getLimit());
        }

        if (Objects.nonNull(request.getOffset())) {
            example.setOffset(request.getOffset());
        }

        if (StringUtils.isBlank(request.getOrderCause()) && StringUtils.isNotBlank(request.getOrderDir())) {
            example.setOrderByClause(String.format("%s %s", request.getOrderCause(), request.getOrderDir()));
        }
        return mapper.selectByExample(example);
    }

    private CompressionCodeExample generateExamples(QueryCompressCodesRequest request) {
        CompressionCodeExample example = new CompressionCodeExample();
        CompressionCodeExample.Criteria criteria = example.createCriteria();
        if (request.getStatus() != null) {
            criteria.andStatusEqualTo(request.getStatus());
        }
        return example;
    }

    public void insertOne(CompressionCode compressionCode) {
        compressionCode.setCreateTime(new Date());
        mapper.insertSelective(compressionCode);
        log.info("[DB]Insert new compression code: {}",compressionCode.getCompressionCode());
    }

    public CompressionCode queryByCode(String code) {
        CompressionCodeExample example = new CompressionCodeExample();
        CompressionCodeExample.Criteria criteria = example.createCriteria();
        criteria.andCompressionCodeEqualTo(code);
        return Iterables.getFirst(mapper.selectByExample(example), null);
    }


    public void updateByPrimaryKey(CompressionCode compressionCode) {
        compressionCode.setUpdateTime(new Date());
        mapper.updateByPrimaryKeySelective(compressionCode);
        log.info("[DB]Update compress: {}", JsonUtils.toJSONString(compressionCode));
    }


    public void batchUpdateStatus(List<String> codes, Byte status) {
        if (CollectionUtils.isEmpty(codes)) {
            return;
        }

        CompressionCode record = new CompressionCode();
        record.setUpdateTime(new Date());
        record.setStatus(status);

        CompressionCodeExample example = new CompressionCodeExample();
        CompressionCodeExample.Criteria criteria = example.createCriteria();
        criteria.andCompressionCodeIn(codes);
        mapper.updateByExampleSelective(record, example);
        log.info("[DB]Batch update compress:{}->{}", codes, JsonUtils.toJSONString(record));
    }

    public long countByStatus(byte status) {
        CompressionCodeExample example = new CompressionCodeExample();
        CompressionCodeExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(status);
        return mapper.countByExample(example);
    }

    public void batchInsert(List<CompressionCode> records){
        myCompressionCodeMapper.batchInsert(records);
    }
}
